﻿$(function() {
	$('#GroupList').combobox({
		valueField: 'GroupID',
		textField: 'GroupName'
	});


	dlg_Edit = $('#Dlg-Edit').dialog({
		closed: true,
		modal: true,
		toolbar: [{
			text: '保存',
			iconCls: 'icon-add',
			handler: saveData
		}, '-', {
			text: '关闭',
			iconCls: 'icon-no',
			handler: function() {
				dlg_Edit.dialog('close');
			}
		}]
	});

	dlg_Edit_form = dlg_Edit.find('form');

	autoResize({ dataGrid: '#grid', callback: Initlist });
	$('body').layout();
});

function createColumnMenu() {
    var tmenu = $('<div id="tmenu" style="width:100px;"></div>').appendTo('body');
    var fields = grid.datagrid('getColumnFields');
    for (var i = 0; i < fields.length; i++) {
        $('<div iconCls="icon-ok"/>').html(fields[i]).appendTo(tmenu);
    }
    tmenu.menu({
        onClick: function (item) {
            if (item.iconCls == 'icon-ok') {
                grid.datagrid('hideColumn', item.text);
                tmenu.menu('setIcon', {
                    target: item.target,
                    iconCls: 'icon-empty'
                });
            } else {
                grid.datagrid('showColumn', item.text);
                tmenu.menu('setIcon', {
                    target: item.target,
                    iconCls: 'icon-ok'
                });
            }
        }
    });
}

var grid;
var dlg_Edit;
var dlg_Edit_form;
var AuthorityID;
var operate;
var row;
function saveData()
{
    var param = { AssemblyName:"OA.Bll.Authority", ClassName: "OA.Bll.Authority.RolesPresenter", Method: "", Options: {} };
    if(operate =="edit")
    {
         param .Method ="Update";
         param.Options["Id"]=row.Id;
//         param.Options["RoleGroupID"]=$('#GroupList').combobox('getValue');
         param.Options["RoleName"]=$("#txtName").val();
         param.Options["RoleDescription"]=$("#txtDescription").val();
         param.Options["RoleOrder"]=$("#txtorder").val();
    }
    else 
    {
         param .Method ="Add";
//         param.Options["RoleGroupID"]=$('#GroupList').combobox('getValue');
         param.Options["RoleName"]=$("#txtName").val();
         param.Options["RoleDescription"]=$("#txtDescription").val();
         param.Options["RoleOrder"]=$("#txtorder").val();
     }
       if (dlg_Edit_form.form('validate')) {
           $.ajax({
            type: "post",
            url: '../../Handler/AjaxHandle.ashx',
            data: $.toJSON(param),
            dataType: 'json',
            success: function (data) {
                 if (data.flag) {
                grid.datagrid('reload');
                    $.messager.alert('提示', data.msg, 'info');
                dlg_Edit.dialog('close');
                } else {
                    $.messager.alert('错误', data.msg, 'error');
                        }
            },
            error: function (data) {
                 $.messager.alert("信息提示", data.msg, "error");
                }
        });
        }
}



function add() {
//    $("#TxtUserName").unbind("focus");
    show("新增角色");
    dlg_Edit.dialog('open');
    dlg_Edit_form.form('clear');
     operate="add";
//    dlg_Edit_form.url = 'Handler/UserManageHandler.ashx?action=add';

}




function show(mytitle){
    $("#Dlg-Edit").dialog({
        title: mytitle
    });}
function edit() {

    rows = grid.datagrid('getSelections');
    var num = rows.length;
    if (num == 0) {
        Msgslide('请选择一条记录进行操作!'); //$.messager.alert('提示', '请选择一条记录进行操作!', 'info');
        return;
    }
    else if (num > 1) {
        Msgfade('您选择了多条记录,只能选择一条记录进行修改!'); //$.messager.alert('提示', '您选择了多条记录,只能选择一条记录进行修改!', 'info');
        return;
    }
    else {
//        $("#GroupList").combobox('setText',rows [0].GroupName);
//        $("#GroupList").combobox('setValue',rows [0].GroupID);
        $("#txtName").val(rows[0].RoleName);
        $("#txtDescription").val(rows[0].RoleDescription);
        $("#txtorder").val(rows[0].RoleOrder);
        show("修改角色");
        isedit =true;
        row =rows [0];
        dlg_Edit.dialog('open');
        operate="edit";
  }
}


function del() {
    var arr = getSelectedArr();
    if (arr.length > 0) {
        $.messager.confirm('提示信息', '您确认要删除吗?', function (data) {
            if (data) {
                var p = { AssemblyName:"OA.Bll.Authority", ClassName: "OA.Bll.Authority.RolesPresenter", Method: "Delete", Options: {} };
                p.Options ["Id"]=arr2str(arr);
                $.ajax({
                    url: '../../Handler/AjaxHandle.ashx' ,
                    type: 'post',
                    dataType: 'json',
                    data:$.toJSON(p),
                    success: function (r) {
                        if (r.flag=="True") {
                            grid.datagrid('reload');
                            grid.datagrid('clearSelections');
                        } else {
                            $.messager.alert('错误',r .msg , 'error');
                        }
                    },
                 error: function (r) {
                    $.messager.alert("信息提示", r.msg, "error");
                  }
                });
            }
        });
    } else {
        Msgshow('请先选择要删除的记录。');
    }
}

function getSelectedArr() {
    var ids = [];
    var rows = grid.datagrid('getSelections');
    for (var i = 0; i < rows.length; i++) {
        ids.push(rows[i].RoleID);
    }
    return ids;
}
function getSelectedID() {
    var ids = getSelectedArr();
    return ids.join(',');
}
function arr2str(arr) {
    return arr.join(',');
}


function Msgshow(msg) {
	$.messager.alert('提示', msg, 'info');
}

function Msgslide(msg) {
    $.messager.show({
        title: '提示',
        msg: msg,
        timeout: 3000,
        showType: 'slide'
    });
}
function Msgfade(msg) {
    $.messager.show({
        title: '提示',
        msg: msg,
        timeout: 3000,
        showType: 'fade'
    });
}

function Initlist(size) {
	var p = { AssemblyName: "OA.Bll.Authority", ClassName: "OA.Bll.Authority.RolesPresenter", Method: "Paged", Options: { } };
	grid = $('#grid').datagrid({
		iconCls: 'icon-save',
		methord: 'post',
		url: '../../Handler/AjaxHandle.ashx',
		sortName: 'AuthorityID',
		queryParams: { "json": $.toJSON(p) },
		sortOrder: 'desc',
		idField: 'AuthorityID',
		width: size.width,
		height: size.height,
		pageSize: Math.round(size.height / 25),
		pageList: [Math.round(size.height / 25)],
		columns: [[
			{ field: 'Id', title: '角色ID', width: 200, hidden: false },
			{ field: 'RoleName', title: '角色名', width: 200, sortable: true },
			{ field: 'RoleDescription', title: '说明', width: 200, sortable: true },
			{ field: 'RoleOrder', title: '排序', width: 200, sortable: true }
		]],
		toolbar: [{
			text: '新增',
			iconCls: 'icon-add',
			handler: add
		}, '-', {
			text: '修改',
			iconCls: 'icon-edit',
			handler: edit
		}, '-', {
			text: '删除',
			iconCls: 'icon-remove',
			handler: del
		}],
		pagination: true,
		rownumbers: true,
		singleSelect: true,
		onHeaderContextMenu: function(e, field) {
			e.preventDefault();
			if (!$('#tmenu').length) {
				createColumnMenu();
			}
			$('#tmenu').menu('show', {
				left: e.pageX,
				top: e.pageY
			});
		}
	});
}
